import request from '@/utils/request'
import { checkModuleKey } from '@/core/app'

/**
 * 根据权限切换接口地址
 * @param {String} key 权限标识
 */

function switchUrl (key) {
  let prefix = `/store`
  let isGms = false
  if (checkModuleKey(key)) {
    prefix = `/store/gms`
    isGms = true
  }
  switch(key) {
    case 'ly_category_info':
      return isGms ? `${prefix}/categories/tree` : `${prefix}/goods/categories/tree`
    case 'ly_store_goods_sale_status_update':
      return isGms ? `${prefix}/store-goods/sale-status/batch` : `${prefix}/goods/sale-status/batch`
    case 'ly_store_goods_sku_update':
      return isGms ? `${prefix}/store-goods/store-skus/modify` : `${prefix}/goods/modify`
    case 'ly_store_goods_sku_temp_offline':
      return isGms ? `${prefix}/store-goods/store-skus/temp-offline` : `${prefix}/goods/temporary-off`
  }
}

/**
 * 分类tree
 * */
export const categoryTree = (param) => {
  return request.get(switchUrl('ly_category_info'), param, { load: false})
}

/**
 * 商品列表查询
 * */
export const getGoodsList = (param, option) => {
  return request.get('/store/gms/store-goods/page', param, option)
}

/**
 * 门店商品信息修改（同步价格/库存状态，修改价格/库存）
 * */
export const goodsUpdate = (param) => {
  return request.post(switchUrl('ly_store_goods_sku_update'), param)
}

/**
 * 批量上/下架商品
 * */
export const batchSaleStatus = (param) => {
  return request.post(switchUrl('ly_store_goods_sale_status_update'), param)
}

/**
 * 临时上/下架商品
 * */
export const temporaryOff = (param, option) => {
  const options = {
    isPrompt: true, //（默认 true 说明：本接口抛出的错误是否提示）
    load: false, //（默认 true 说明：本接口是否提示加载动画）
    ...option
  }
  return request.post(switchUrl('ly_store_goods_sku_temp_offline'), param, options)
}

/**
 * 新品上线列表查询
 * */
export const getAppliesList = (param, option) => {
  return request.get(`/store/gms/offline-sku/applies/page`, param, option)
}

/**
 * 新品上线申请
 * */
export const applyGoodsLaunch = (id, option) => {
  return request.post(`/store/gms/offline-sku/applies/${id}`, {}, option)
}

